package com.lkm.market2_0.m_imple.steelyardserver.server.sendtask;

import com.lkm.frame.P;
import com.lkm.frame.task2.ITaskExecutor;
import com.lkm.frame.task2.TaskManager;
import com.lkm.hardware_connect.IHardwareControler;
import com.lkm.javatool.CollectionHelp;
import com.lkm.javatool.NumberHelp;
import com.lkm.market2_0.help.UIP;
import com.lkm.market2_0.m.steelyardserver.GoodsButtonInfoTo;
import com.lkm.market2_0.m_imple.steelyardserver.server.AbsSendWriteTask;
import com.lkm.market2_0.m_imple.steelyardserver.server.ReceiveTask;

import java.io.UnsupportedEncodingException;
import java.util.Arrays;
import java.util.List;



public class SetGoodsButton3134Task extends AbsSendWriteTask {

    

    public static boolean isUseTheVersion(int version){

        return version >=3134 ;
    }
    

    public SetGoodsButton3134Task(ITaskExecutor<Object[], Void, Boolean> taskExecutor, TaskManager mTaskManager, IHardwareControler bluetoothSocket, ReceiveTask mReceiveTask, List<GoodsButtonInfoTo> goodsInfoTos) {
        super(taskExecutor, SetGoodsButton3134Task.class.getName(), mTaskManager, bluetoothSocket,mReceiveTask);
        setParam(new Object[]{goodsInfoTos});
    }

    @Override
    protected byte onGetRequestCMD(int[] outDataSize) {
        List<GoodsButtonInfoTo> goodsInfoTos=(List<GoodsButtonInfoTo>)getParam()[0];

        
        int dataSize=goodsInfoTos.size()*16;

        outDataSize[0]=dataSize;
        return 0x04;
    }

    @Override
    protected byte[] onGetSendDataItem(int index) {
        List<GoodsButtonInfoTo> goodsInfoTos=(List<GoodsButtonInfoTo>)getParam()[0];
        if(goodsInfoTos==null)return null;

        int dataitemLength=16;


        
        int pageSize = (32/dataitemLength);

        int startIndex=pageSize*index;
        int endIndex=pageSize*(index+1);
        if(startIndex>=goodsInfoTos.size()){
            return null;
        }
        endIndex=Math.min(goodsInfoTos.size(),endIndex);

        int curPageSize=endIndex-startIndex;

        byte [] data= new byte[32];
        Arrays.fill(data,(byte)0x20);
        
        if(curPageSize*dataitemLength!=data.length){
            
        }

        for (int i = startIndex; i < endIndex; i++) {

            GoodsButtonInfoTo goodsInfoTo= goodsInfoTos.get(i);
            String name = holdCharWidth(12,goodsInfoTo.getName());

            int price = ((int) (goodsInfoTo.getPrice()*1000))/10;

            byte[] nameBytes=null;
            try {
                nameBytes=name.getBytes("gbk");
            } catch (UnsupportedEncodingException e) {
                nameBytes=new byte[12];
                e.printStackTrace();
            }
            
            byte[] priceBytes= NumberHelp.intToBytesDesc(price);

            int start=(i-startIndex)*dataitemLength;

            for (int j = 0; j < 12; j++) {
                data[start+j]=nameBytes[j];
            }
            data[start+12]=priceBytes[0];
            data[start+12+1]=priceBytes[1];
            data[start+12+2]=priceBytes[2];
            data[start+12+3]=priceBytes[3];

        }



        byte checkNum=0;
        for(int i=0;i<data.length;i++){
            checkNum+=data[i];
        }

        
        data = CollectionHelp.concatByte(new byte[]{(byte)0x55, (byte) 0xAA, (byte)0x26, (byte)0x00},data);
        data = CollectionHelp.concatByte(data,new byte[]{(byte)00, (byte) 0x0D});
        data[data.length - 2] = checkNum;

        return data;
    }

    @Override
    public void onPreExecute() {
        super.onPreExecute();

        if(P.isDebug()) {
            P.p("=======开始设置菜品按钮");
            if(UIP.isPrint()){
                UIP.p("Send设置菜品按钮：开始",mReceiveTask==null?"":mReceiveTask.steelyardMac);
            }
        }
    }
    @Override
    public void onCancel() {
        super.onCancel();
        if (P.isDebug()) {
            P.p("=======开始设置菜品按钮取消了");
            if (UIP.isPrint()) {
                UIP.p("Send设置菜品按钮：取消",mReceiveTask==null?"":mReceiveTask.steelyardMac);

            }
        }
    }

    @Override
    public void onFinish(Boolean aBoolean) {
        super.onFinish(aBoolean);

        if (P.isDebug()) {
            P.p("======设置菜品按钮是否成功："+aBoolean);;
            if (UIP.isPrint()) {
                UIP.p("Send设置菜品按钮：成功？"+aBoolean,mReceiveTask==null?"":mReceiveTask.steelyardMac);

            }
        }
    }
}